package com.lins.common.mp.controller;

import com.lins.common.common.enums.SuccessCode;
import com.lins.common.common.exception.BusinessException;
import com.lins.common.common.result.Result;
import com.lins.common.common.util.JacksonUtils;
import com.lins.common.mp.dto.BaseDto;
import com.lins.common.mp.dto.CommonBaseDto;
import com.lins.common.mp.result.RespResult;
import lombok.extern.slf4j.Slf4j;

/**
 * @author linsir
 * @title: BaseController
 * @projectName lins
 * @description: 基类, 所有的controller应继承该类
 * @date 2021/12/10 0:06
 */

@Slf4j
public abstract class BaseController {

    /**
     * 简化写法，已包括：log、异常
     *
     * @param logName  log名称
     * @param dto      参数
     * @param callable 回调
     * @return
     */

    protected <B, D extends CommonBaseDto> Result exec(String logName, D dto, ControllerCallable callable) {
        return exec(logName, dto, true, callable);
    }

    /**
     * 简化写法，已包括：log、异常
     *
     * @param logName    log名称
     * @param dto        参数
     * @param showResult 是否展示结果到log，针对list记录过大时，应该false
     * @param callable   回调
     * @return
     */

    protected <B,D extends CommonBaseDto> Result exec(String logName, D dto, boolean showResult, ControllerCallable callable){
        Result result =null;
        try {
            log.info(logName+" param:{} "+" OperationType:{}", JacksonUtils.toJSONString(dto),dto.getOperationType().getMsg());
            result = callable.execute();
        }catch (BusinessException e)
        {
            log.error("业务异常");
        }catch (Exception e)
        {
            /*系统异常*/
            log.error(logName + "异常", e);
        }
        if (showResult)
        {
            log.info(logName + " result:{}", JacksonUtils.toJSONString(result));
        }
        else
        {
            log.info(logName + " result:{}", JacksonUtils.toJSONString(SuccessCode.SUCCESS));

        }
        return result;
    }
}
